package cn.circle.system.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.circle.system.util.DataGrid;
import cn.circle.system.util.HqlHelper;
import cn.circle.system.util.PageHelper;
import cn.circle.system.dao.ISysUserDao;

import com.graduation.system.model.SysUser;
import cn.circle.system.service.ISysUserService;

@Repository
public class SysUserService implements ISysUserService{

	
	@Autowired
	public ISysUserDao sysUserDao;
	
	DataGrid dg = new DataGrid();
	
	
	public DataGrid datagrid(PageHelper ph, SysUser c ,String school,String userName) {

		DataGrid dg = new DataGrid();
		List<SysUser> cml = new ArrayList<SysUser>();
		// 用来放置查询的条件
		Map<String, Object> params = new HashMap<String, Object>();
		// 注意下面为HQL语句，里面sql的关键字大小写不敏感，其它是敏感的,from后面是model类名称
		StringBuffer hql = new StringBuffer("from SysUser t where 1=1 ");
		// 以下为查询条件
		if (school != null && !"".equals(school) && !"全部".equals(school)) {
			System.out.println(school);
			try {
				school = URLDecoder.decode(c.getSchool(), "GBK");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			params.put("school", "%" + school + "%");
			hql.append(" and t.school like :school ");
		}
		if (userName != null && !"".equals(userName)) {
			System.out.println(userName);
			String custName = null;
			try {
				custName = URLDecoder.decode(c.getUserName(), "GBK");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			params.put("userName", "%" + custName + "%");
			hql.append(" and t.userName like :userName ");
		}
		hql = HqlHelper.orderHql(hql, ph); // 排序
		List<SysUser> l = sysUserDao.find(hql.toString(), params, ph.getPage(), ph.getRows());
		dg.setRows(l);
		dg.setTotal(sysUserDao.count("select count(*) " + hql, params));
		return dg;
	}

	public List<SysUser> getUser(String id) {
		// TODO Auto-generated method stub
		return sysUserDao.getUser(id);
	}

	public boolean save(SysUser sysUser) {
		// TODO Auto-generated method stub
		sysUserDao.save(sysUser);
		return true;
	}

	public boolean remove(String depId) {
		// TODO Auto-generated method stub
		sysUserDao.removeAll(depId);
		System.out.println("2");
		return false;
	}

	public boolean delete(String id) {
		// TODO Auto-generated method stub
		sysUserDao.deleteAll(id);
		return false;
	}

	public boolean update(SysUser c) {
		// TODO Auto-generated method stub
		sysUserDao.updataAll(c);
		System.out.println("saveOrUpdate");
		return true;
	}

}
